package com.king.wms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.king.wms.mapper.LocMapper;
import com.king.wms.mapper.ReceiptMapper;
import com.king.wms.mapper.SkuMapper;
import com.king.wms.pojo.Loc;
import com.king.wms.pojo.Receipt;
import com.king.wms.pojo.Sku;


@Service
@Transactional
public class ReceiptService {

	@Autowired
	private ReceiptMapper receiptMapper;
	@Autowired
	private SkuMapper skuMapper;
	@Autowired
	private LocMapper locMapper;

	public String insert(Receipt receipt) {
		Sku sku = skuMapper.findSkuBySku_id(receipt.getSku_id());
		receipt.setExpenses(sku.getPurchasePrice()*receipt.getQuantity());
		Loc loc = locMapper.selectByPrimaryKey(receipt.getLoc_id());
		Integer quantity = receipt.getQuantity();
		Integer remain = loc.getStackremianed();
		Integer used;

		if( loc.getStackremianed() >= quantity){
			
			if(sku.getStorer_id().equals(receipt.getStorer_id()) && sku.getLoc_id().equals(receipt.getLoc_id())){
				Integer quantityNew =  sku.getQuantity() + receipt.getQuantity();
				sku.setQuantity(quantityNew);;
				System.out.println("=========================="+sku);
				System.out.println("----------------------------------------hahhahahahah");
				skuMapper.update(sku);
			}else{
				System.out.println("+++++++++++++++++++pppppppp+++++");
				Integer conut = skuMapper.countAll();
				sku.setSku_id(conut+1);
				sku.setQuantity(quantity);
				sku.setLoc_id(receipt.getLoc_id());
				sku.setStorer_id(receipt.getStorer_id());
				skuMapper.insert(sku);
			}
			
			remain = loc.getStackremianed() - quantity;
			used = loc.getStackuesd() + quantity;
			loc.setStackremianed(remain);
			loc.setStackuesd(used);
			receiptMapper.insert(receipt);
			return "success";
		}else{
			return "本次收货订单需要 <h3>"+quantity+"</h3> 个存放单元,该 "+loc.getRegion()+" 区域库剩余 "+remain+" 个存放单元,库存量不足,请换一个库存放.";
		}
		
		
	}

	public List<Receipt> findAll() {
		
		return receiptMapper.findAll();
	}

	public void delete(Integer receipt_id) {
		
		receiptMapper.deleteByPrimaryKey(receipt_id);
	}

	
	public Receipt findReceiptByPrimaryKey(Integer receipt_id) {
		return receiptMapper.selectByPrimaryKey(receipt_id);
	}

	public List<Receipt> findReceived() {
		// TODO Auto-generated method stub
		return receiptMapper.findReceived();
	}

	public List<Receipt> findUnReceived() {
		// TODO Auto-generated method stub
		return receiptMapper.findUnReceived();
	}

	public String update(Receipt receipt) {
		
		Sku sku = skuMapper.findSkuBySku_id(receipt.getSku_id());
		receipt.setExpenses(sku.getPurchasePrice()*receipt.getQuantity());
		Loc loc = locMapper.findByLoc_id(receipt.getLoc_id());
		
		int remain = loc.getStackremianed();
		int used = loc.getStackuesd();
		int quantity = receipt.getQuantity();
		
		if( quantity > remain){
			return "该  "+loc.getRegion()+" 区域存放仅 "+remain+" 个单元空间存放,需要存放  "+quantity+" 个单元的商品,库存不足,请重新操作";
		}else{
			remain = remain - quantity;
			used = used + quantity;
			loc.setStackremianed(remain);
			loc.setStackuesd(used);
			receiptMapper.update(receipt);
			locMapper.update(loc);
			return "success";
		}
		
	}

	public void updateStatus(Integer receipt_id) {
		// TODO Auto-generated method stub
		receiptMapper.updateStatus(receipt_id);
	}

	public Receipt findReceiptByReceipt_id(Integer receipt_id) {
		// TODO Auto-generated method stub
		return receiptMapper.findReceiptByReceipt_id(receipt_id);
	}

}
